/**
 * 
 */
package co.edu.uniandes.mdd.project1.sample.behavior;

import co.edu.uniandes.mdd.project1.sample.domain.*;
import co.edu.uniandes.mdd.project1.sample.util.*;

/**
 * @author Leonardo
 * 
 */
public class EnemyAutomaticBehavior implements Runnable {

	private Enemy enemy;
	private Board board;

	public EnemyAutomaticBehavior(Enemy enemy, Board board) {
		this.enemy = enemy;
		this.board = board;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		boolean continuar = true;
		while (continuar) {
			try {
				boolean movio = false;
				if (enemy.getDirection() == ConstantsDirection.RIGHT) {
					if (board.evaluarColision(enemy.getPositionX() + 1,
							enemy.getPositionY(), enemy)) {
						enemy.setPositionX(enemy.getPositionX() + 1);
						movio = true;
					}
				}else if (enemy.getDirection() == ConstantsDirection.LEFT) {
					if (board.evaluarColision(enemy.getPositionX() - 1,
							enemy.getPositionY(), enemy)) {
						enemy.setPositionX(enemy.getPositionX() - 1);
						movio = true;
					}
				}else if (enemy.getDirection() == ConstantsDirection.UP) {
					if (board.evaluarColision(enemy.getPositionX(),
							enemy.getPositionY()  - 1, enemy)) {
						enemy.setPositionY(enemy.getPositionY() - 1);
						movio = true;
					}
				}else if (enemy.getDirection() == ConstantsDirection.DOWN) {
					if (board.evaluarColision(enemy.getPositionX(),
							enemy.getPositionY() + 1, enemy)) {
						enemy.setPositionY(enemy.getPositionY() + 1);
						movio = true;
					}
				}else{
					enemy.setDirection((int)(1 + Math.random()*4));
				}
				if(!movio){
					enemy.setDirection((int)(1 + Math.random()*4));
//					System.out.println("*Enemy["+enemy.getPosition()+"] = "+enemy.getDirection());
				}
//				System.out.println("Enemy["+enemy.getPosition()+"] = ["+enemy.getPositionX()+","+enemy.getPositionY()+"] , Dir = "+enemy.getDirection());
				Thread.sleep(1000);
			} catch (Exception e) {
				e.printStackTrace();
				continuar = false;
			}
		}

	}

}
